package com.foreveross.common;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.foreveross.util.StringUtil;

public class SQLUtil {

	@PersistenceContext(unitName = "HRTenement")
	private EntityManager entityManager;

	public <T> void save(T entity) throws Exception {
		entityManager.persist(entity);
	}

	public <T> void remove(T entity) throws Exception {
		entityManager.remove(entity);
	}

	public Object findById(Class clazz, String id) throws Exception {
		return entityManager.find(clazz, id);
	}

	public Object findByIntId(Class clazz, int id) throws Exception {
		return entityManager.find(clazz, id);
	}

	public <T> T updateObject(T t) throws Exception {
		return entityManager.merge(t);
	}

	public <T> T updateObject(T t, String id) throws Exception {
		T obj = (T) findById(t.getClass(), id);
		obj = StringUtil.setNewToOldObject(obj, t);
		return entityManager.merge(obj);
	}

	public void addAuthorityStr(StringBuffer str, String userId,
			String optionName) {
		if (userId != null && !"".equals(userId)) {
			str.append(" and  model."
					+ optionName
					+ " in (select rh.hrHouses.id from SysRoleHouse rh where rh.sysRole.id in (select sysRole.id from  SysUserRole ur where ur.sysUser.id='"
					+ userId + "'))");
			
		}
	}
	

	/**
	 * 查询所有数据封装
	 * 
	 * @param queryString
	 * @param rowStartIdxAndCount
	 * @return
	 * @throws Exception
	 */
	public List<?> findObjectAll(final String queryString,
			final int... rowStartIdxAndCount) throws Exception {

		Query query = entityManager.createQuery(queryString);

		if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0) {
			int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
			if (rowStartIdx > 0) {
				query.setFirstResult(rowStartIdx);
			}

			if (rowStartIdxAndCount.length > 1) {
				int rowCount = Math.max(0, rowStartIdxAndCount[1]);
				if (rowCount > 0) {
					query.setMaxResults(rowCount);
				}
			}
		}
		return query.getResultList();
	}

	/**
	 * 查询所有数据封装
	 * 
	 * @param queryString
	 * @param rowStartIdxAndCount
	 * @return
	 * @throws Exception
	 */
	public List<?> findObjectStrAll(final String queryString,
			final String... rowStartIdxAndCount) throws Exception {

		Query query = entityManager.createQuery(queryString);

		if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 1
				&& rowStartIdxAndCount[0] != null
				&& rowStartIdxAndCount[1] != null) {
			try {
				int rowStartIdx = Math.max(0,
						Integer.parseInt(rowStartIdxAndCount[0]));
				if (rowStartIdx > 0) {
					query.setFirstResult(rowStartIdx);
				}

				if (rowStartIdxAndCount.length > 1) {
					int rowCount = Math.max(0,
							Integer.parseInt(rowStartIdxAndCount[1]));
					if (rowCount > 0) {
						query.setMaxResults(rowCount);
					}
				}
			} catch (Exception e) {
			}
		}
		return query.getResultList();
	}
	
	/**
	 * HQL查询返回对象
	 * 
	 * @param hql
	 * @return
	 * @throws Exception
	 */
	public Object findObejctByHql(final String hql) throws Exception {
		Query query = entityManager.createQuery(hql);
		return query.getSingleResult();
	}

	public Object findObejctBySQL(final String sql) throws Exception {
		Query query = entityManager.createNativeQuery(sql);
		return query.getSingleResult();
	}

	/**
	 * sql查询列表
	 * 
	 * @param sql
	 * @return
	 * @throws Exception
	 */

	public List findObejctBySQL(final String sql, final Class clazz,
			final int... rowStartIdxAndCount) throws Exception {

		Query query = null;
		if (clazz != null)
			query = entityManager.createNativeQuery(sql, clazz);
		else
			query = entityManager.createNativeQuery(sql);

		if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0) {
			int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
			if (rowStartIdx > 0) {
				query.setFirstResult(rowStartIdx);
			}

			if (rowStartIdxAndCount.length > 1) {
				int rowCount = Math.max(0, rowStartIdxAndCount[1]);
				if (rowCount > 0) {
					query.setMaxResults(rowCount);
				}
			}
		}
		return query.getResultList();
	}

	public List findObejctByHQL(final String hql) throws Exception {
		Query query = entityManager.createQuery(hql);
		return query.getResultList();
	}


	public void updateObejctBySql(final String sql) throws Exception {
		Query query = entityManager.createNativeQuery(sql);
		query.executeUpdate();
	}

	public void updateObejctByHql(final String hql) throws Exception {
		Query query = entityManager.createQuery(hql);
		query.executeUpdate();
	}
}
